package com.rr.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpoint;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;

/**
 * @author zhengshaobiao
 * @description 华为云mq配置
 **/
@Configuration
@Slf4j
@Import({HwyRabbitProperties.class})
//@ConditionalOnProperty(prefix = "bg.hwy.mq", name = "enable",havingValue = "true")
public class HwyMqConfig {

    @Autowired
    @Qualifier(value = "hwyRabbitProperties")
    private HwyRabbitProperties hwyRabbitProperties;

    /**
     * hwy mq配置
     *
     * @return
     */
    @Bean(name = "hwyConnectionFactory")
    public ConnectionFactory hwyConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(hwyRabbitProperties.getHost());
        connectionFactory.setPort(hwyRabbitProperties.getPort());
        connectionFactory.setUsername(hwyRabbitProperties.getUsername());
        connectionFactory.setPassword(hwyRabbitProperties.getPassword());
        connectionFactory.setVirtualHost(hwyRabbitProperties.getVirtualHost());
        return connectionFactory;
    }

    @Bean(name = "hwyRabbitTemplate")
    public RabbitTemplate hwyRabbitTemplate(@Qualifier("hwyConnectionFactory") ConnectionFactory hwyConnectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(hwyConnectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }

    @Bean(name = "hwyListenerContainerFactory")
    public SimpleRabbitListenerContainerFactory hwyListenerContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("hwyConnectionFactory") ConnectionFactory hwyConnectionFactory,
            MessageConverter messageConverter) {
        MessagePostProcessor messagePostProcessor = message -> {
            // 2021-01-03 不再记录接受参数，与skywalking-rabbitmq有冲突，不能包含在skywalking的traceId里面
            message.getMessageProperties().setContentType(MediaType.APPLICATION_JSON_VALUE);
            return message;
        };
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(messagePostProcessor);
        factory.setMessageConverter(messageConverter);
        configurer.configure(factory, hwyConnectionFactory);
        return factory;
    }

    public static class SimpleRabbitListenerContainerFactory
            extends org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory {

        private final MessagePostProcessor[] messagePostProcessor;

        public SimpleRabbitListenerContainerFactory(MessagePostProcessor... messagePostProcessor) {
            super();
            this.messagePostProcessor = messagePostProcessor;
        }

        @Override
        protected void initializeContainer(SimpleMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
            super.initializeContainer(instance, endpoint);
            instance.addAfterReceivePostProcessors(messagePostProcessor);
        }
    }



    @Bean("hwyRabbitAdmin")
    public RabbitAdmin hwyRabbitAdmin(@Qualifier("hwyConnectionFactory") ConnectionFactory hwyConnectionFactory) {
        return new RabbitAdmin(hwyConnectionFactory);
    }

}
